package com.glc.itbook;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;



import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.ui.AppBarConfiguration;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.bumptech.glide.Glide;
import com.glc.itbook.bean.BaseUrl;
import com.glc.itbook.bean.Book;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

public class RankListActivity extends AppCompatActivity {

    private ListView rlistView;
    private BaseAdapter adapter;

    private AppBarConfiguration appBarConfiguration;

    private Context appContext;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        appContext = getApplicationContext();
        setContentView(R.layout.activity_rank_list);
        RequestQueue requestQueue= Volley.newRequestQueue(RankListActivity.this);

        rlistView = findViewById(R.id.rank_list);

        JSONObject jsonObject=new JSONObject();
        final BaseUrl baseUrl = (BaseUrl)getApplication();
        String url = baseUrl.getBaseUrl()+"/item/top10";
        JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(Request.Method.GET, url, jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Gson gson = new Gson();
                final Book book = gson.fromJson(jsonObject.toString(), Book.class);
                int currentPage = book.getCurrentPage();
                adapter = new BaseAdapter() {
                    @Override
                    public int getCount() {
                        return book.getItems().size();
                    }

                    @Override
                    public Object getItem(int i) {
                        return null;
                    }

                    @Override
                    public long getItemId(int i) {
                        return 0;
                    }

                    @Override
                    public View getView(final int i, View view, ViewGroup viewGroup) {
                        view = View.inflate(appContext, R.layout.item_booklist, null);
                        ImageView imageView = view.findViewById(R.id.item_image);
                        TextView name = view.findViewById(R.id.item_bookName);
                        final TextView user = view.findViewById(R.id.item_bookUser);
                        TextView info = view.findViewById(R.id.item_bookInfo);
                        TextView down = view.findViewById(R.id.item_bookDown);
                        name.setText("书名:"+book.getItems().get(i).getBook_name());
                        user.setText("作者:"+book.getItems().get(i).getBook_author());
                        info.setText("简介:"+book.getItems().get(i).getBook_info());

                        rlistView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                                Intent intent=new Intent(appContext, XiangQingActivity.class);
                                Bundle bundle=new Bundle();
                                bundle.putString("name",book.getItems().get(i).getBook_name());
                                bundle.putString("author",book.getItems().get(i).getBook_author());
                                bundle.putString("jianjie",book.getItems().get(i).getBook_info());
                                bundle.putString("down",book.getItems().get(i).getBook_download());
                                bundle.putString("img",book.getItems().get(i).getBook_img());
                                intent.putExtras(bundle);
                                startActivity(intent);
                            }
                        });

                        Glide.with(appContext).load(book.getItems().get(i).getBook_img()).into(imageView);

                        return view;
                    }
                };
                rlistView.setAdapter(adapter);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.d("错误", volleyError.toString());
                Toast.makeText(RankListActivity.this, "网络失败", Toast.LENGTH_SHORT).show();
            }
        });
        requestQueue.add(jsonObjectRequest);

    }

}